#include "library/base/convert.h"

namespace Base
{
	//ToInt

	int Convert::ToInt(const String& Object)
	{
		return atoi(Object.GetBuffer());
	}

	int Convert::ToInt(const WString& Object)
	{
		return _wtoi(Object.GetBuffer());
	}

	//ToDouble

	double Convert::ToDouble(const String& Object)
	{
		return atof(Object.GetBuffer());
	}

	double Convert::ToDouble(const WString& Object)
	{
		return _wtof(Object.GetBuffer());
	}

	bool Convert::ToBool(const String& Object)
	{
		if (Object=="true") return true;
		else if (Object=="false") return false;
		else throw exceptionARGUMENT_OUT_OF_RANGE;
	}

	bool Convert::ToBool(const WString& Object)
	{
		if (Object==L"true") return true;
		else if (Object==L"false") return false;
		else throw exceptionARGUMENT_OUT_OF_RANGE;
	}

	//ToString

	String Convert::ToString(const int Object)
	{
		char Buffer[65];
		 _itoa_s(Object, Buffer, 65, 10);
		 Buffer[10]=0;
		 return String(Buffer);
	}

	String Convert::ToString(const double Object)
	{
		char Buffer[_CVTBUFSIZE];
		_gcvt_s(Buffer, _CVTBUFSIZE, Object, 10);
		Buffer[10]=0;
		return String(Buffer);
	}

	String Convert::ToString(const bool Object)
	{
		if (Object) return "true";
		else return "false";
	}

	//ToWString

	WString Convert::ToWString(const char* Object)
	{
		return Convert::ToWString(String(Object));
	}

	WString Convert::ToWString(const String& Object)
	{
		wchar_t* Buffer=new wchar_t[Object.GetLength()+1];
		size_t length=0;
		mbstowcs_s(&length, Buffer, Object.GetLength()+1, Object.GetBuffer(), _TRUNCATE);
		WString Result=Buffer;
		delete[] Buffer;
		return Result;
	}

	WString Convert::ToWString(const int Object)
	{
		wchar_t Buffer[65];
		_itow_s(Object, Buffer, 65, 10);
		Buffer[10]=0;
		return WString(Buffer);
	}

	WString Convert::ToWString(const double Object)
	{
		return Convert::ToWString(Convert::ToString(Object));
	}

	WString Convert::ToWString(const bool Object)
	{
		if (Object) return L"true";
		else return L"false";
	}
};